
var TombPlay = cc.Layer.extend({
    sprite:null,
    _actor:null,
    _texTransparentBatch:null,
    _ZombieTransparentBatch:null,
    _zombieSpeed:3,     /* 3秒钟撞击主角 */
    _gameRound:0,       /* 游戏的关数 */
    lbScore:null,       /* 游戏的得分控件 */
    _tmpScore:0,        /* 分数  */
    _gameState:MW.GAME_STATE.PLAY,
    _menu:null,
    ctor:function () {
        //////////////////////////////
        // 1. super init first
        this._super();
        cc.audioEngine.playMusic(cc.sys.os == cc.sys.OS_WP8 || cc.sys.os == cc.sys.OS_WINRT ? res.ZombieDie_wav : res.play_bg_mp3, true);

        /////////////////////////////
        // 2. add a menu item with "X" image, which is clicked to quit the program
        //    you may modify it.
        // ask the window size
        var size = cc.winSize;

        winSize = cc.director.getWinSize();
        var sp = new cc.Sprite(res.TombBackGround_jpg);
        sp.anchorX = 0;
        sp.anchorY = 0;
        this.addChild(sp, 0, 1);

        cc.spriteFrameCache.addSpriteFrames(res.Actor_plist);
        cc.spriteFrameCache.addSpriteFrames(res.Zombie_plist);

        // TransparentBatch
        var texTransparent = cc.textureCache.addImage(res.Actor_png);
        this._texTransparentBatch = new cc.SpriteBatchNode(texTransparent);
        this.addChild(this._texTransparentBatch);

        var ZombieTransparent = cc.textureCache.addImage(res.Zombie_png);
        this._ZombieTransparentBatch = new cc.SpriteBatchNode(ZombieTransparent);
        this.addChild(this._ZombieTransparentBatch);


        // 画主角
        this._actor = new ActorWalk();
        this._texTransparentBatch.addChild(this._actor, this._actor.zOrder, 1000);

        // 攻击按钮
        var FightUp   = new cc.Sprite(res.AttackBtnUp_png);
        var FightDown = new cc.Sprite(res.AttackBtnDown_png);
        var FightDisable   = new cc.Sprite(res.AttackBtnUp_png);
        var Fight = new cc.MenuItemSprite(FightUp, FightDown, FightDisable, this.onFight, this);
        this._menu = new cc.Menu(Fight);
        this.addChild(this._menu, 1, 1);
        this._menu.x = winSize.width / 2;
        this._menu.y = winSize.height / 6;


        // 得分
        this.lbScore = new cc.LabelBMFont("0", res.arial_14_fnt);
        this.lbScore.attr({
            x: winSize.width/2,
            y: winSize.height/4*3,
            scale: 1
        });
        this.lbScore.textAlign = cc.TEXT_ALIGNMENT_RIGHT;
        this.addChild(this.lbScore, 1000);

        // 产生僵尸的定时器
        this.schedule(this.ifGetNewZombie, MW.ZOMBIE.GEN_INTERVAL);

        this.scheduleUpdate();

    },
    ifGetNewZombie:function () {
        if (this._gameState == MW.GAME_STATE.OVER)
            return;

        /* 先计算当前出现的概率 */

        var Possibility = MW.ZOMBIE.NUM_PER_SEC * MW.ZOMBIE.GEN_INTERVAL;
        if (Math.random() > Possibility)
        {
            this._gameRound ++;
            return;
        }

        var zombie = new Zombie(this._zombieSpeed);
        MW.CONTAINER.ZOMBIES.push(zombie);
        this._ZombieTransparentBatch.addChild(zombie, this._actor.zOrder, 1000);
        this._gameRound ++;

        /* 速度不断的变快  */
        this._zombieSpeed = this._zombieSpeed - 0.05;
        if (this._zombieSpeed < 0.5) this._zombieSpeed = 0.5;
    },
    collide:function (a, b) {
	    var ax = a.x, ay = a.y, bx = b.x, by = b.y;

        var aRect = a.collideRect(ax, ay);
        var bRect = b.collideRect(bx, by);
        return cc.rectIntersectsRect(aRect, bRect);
    },
    update:function(){
        if (this._gameState == MW.GAME_STATE.OVER)
            return;

        this.checkIsCollide();
        this.lbScore.setString(this._tmpScore);
    },
    checkIsCollide:function(){
        var selChild;
        var i=0;

        if (MW.CONTAINER.ZOMBIES.length >0 ){
            selChild = MW.CONTAINER.ZOMBIES[0];
            var ax = selChild.x, ay = selChild.y, bx = this._actor.x, by = this._actor.y;
            var ZombieRect = selChild.collideRect(ax, ay);
            var ActorRect = this._actor.collideRect(bx, by);
            if (cc.rectIntersectsRect(ActorRect, ZombieRect)){  /* 僵尸撞到了主角 */
                 MW.CONTAINER.ZOMBIES.shift();
                 //selChild.destroy(this._texTransparentBatch);
                 selChild.bite();
                 this._gameState = MW.GAME_STATE.OVER;
                 for (i= 0; i<MW.CONTAINER.ZOMBIES.length; i++){
                    var zombie = MW.CONTAINER.ZOMBIES[i];
                    zombie.stopAction(zombie.moveAction);
                 }
                 this._actor.dead();
                 this._menu.setEnabled(false);
            }
        }
    },
    onFight:function (pSender) {
        this._actor.fight();
        var selChild;
        var i=0;

        if (MW.CONTAINER.ZOMBIES.length >0 ){
            selChild = MW.CONTAINER.ZOMBIES[0];
            var ax = selChild.x, ay = selChild.y, bx = this._actor.x, by = this._actor.y;
            var ZombieRect = selChild.collideRect(ax, ay);
            var ActorRect = this._actor.fightRect(bx, by);
            if (cc.rectIntersectsRect(ActorRect, ZombieRect)){  /* 与挥刀有碰撞，则需要杀死僵尸 */
                 MW.CONTAINER.ZOMBIES.shift();
                 this._tmpScore++;
                 selChild.killed(this._texTransparentBatch);
            }
        }

    }
});

var TombPlayScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        var layer = new TombPlay();
        this.addChild(layer);
    }
});

